package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

/**
 * @author cxl
 * @date 2020-02-22 20:59
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private AddressService addressService;


    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        String originalFilename = imgFile.getOriginalFilename();//获得上传文件的原始文件名,包含后缀名
        int index = originalFilename.lastIndexOf(".");//获得最后一个截取符
        String substring = originalFilename.substring(index-1);//截取到后缀名
        String fileName = UUID.randomUUID().toString()+substring;
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);//上传文件
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true,MessageConstant.UPLOAD_SUCCESS,fileName);
    }


    //分页展示
    @RequestMapping("/findaddressPage")
    public PageResult findaddressPage(@RequestBody QueryPageBean queryPageBean){
        return addressService.pageQuery(queryPageBean);
    }

    //添加
    @RequestMapping("/add")
    public Result add(@RequestBody Address address){
        Boolean flag = addressService.Add(address);
        if (flag){
            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        }else{
            return new Result(false,MessageConstant.ADD_ADDRESS_FAIL);
        }
    }

    //更新弹窗回显数据
    @RequestMapping("/findAddressById")
    public Result findAddressById(int id){
        Address address = addressService.findAddressById(id);
        if (address!=null){
            return new Result(true,MessageConstant.QUERY_ADDRESS_SUCCESS,address);
        }else{
            return new Result(false,MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }

    //更新
    @RequestMapping("/edit")
    public Result edit(@RequestBody Address address){
        try {
            addressService.edit(address);
            return new Result(true,MessageConstant.Edit_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.Edit_ADDRESS_FAIL);
        }
    }




    @RequestMapping("/delete")
    public Result delete(int id){

        Boolean flag = addressService.delete(id);
        if (flag){
            return new Result(true,MessageConstant.DELETE_ADDRESS_SUCCESS);
        }else{
            return new Result(false,MessageConstant.DELETE_ADDRESS_FAIL);
        }
    }

}
